﻿using System;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using AdferInternals;

namespace Adfer
{
    public partial class ComponentViewer : Form
    {
        private Bitmap _bitmap;
        private readonly static string[] ColorArray;
        public ComponentViewer(Bitmap bitmap)
        {
            InitializeComponent();
            _bitmap = bitmap;

            comboColor.Items.AddRange(ColorArray);
        }

        static ComponentViewer()
        {
            ColorArray = typeof(Color).GetProperties(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public)
                       .Select(c => ((Color)c.GetValue(null, null)).Name)
                       .ToArray();
        }

        private void btnRed_Click(object sender, EventArgs e)
        {
            var matrix = BitmapHelper.GetRedMatrix(_bitmap);
            var bitmap = BitmapHelper.GetBitmapFromRed(matrix);

            var viewer = new BitmapViewer(bitmap);
            viewer.Show();
        }

        private void btnGreen_Click(object sender, EventArgs e)
        {
            var matrix = BitmapHelper.GetGreenMatrix(_bitmap);
            var bitmap = BitmapHelper.GetBitmapFromGreen(matrix);

            var viewer = new BitmapViewer(bitmap);
            viewer.Show();
        }

        private void btnBlue_Click(object sender, EventArgs e)
        {
            var matrix = BitmapHelper.GetBlueMatrix(_bitmap);
            var bitmap = BitmapHelper.GetBitmapFromBlue(matrix);

            var viewer = new BitmapViewer(bitmap);
            viewer.Show();
        }

        private void btnColor_Click(object sender, EventArgs e)
        {
            Color.FromName(comboColor.SelectedText);
        }

        private void btnAlpha_Click(object sender, EventArgs e)
        {
            var matrix = BitmapHelper.GetAlphaMatrix(_bitmap);
            var bitmap = BitmapHelper.GetBitmapFromAlpha(matrix, Color.Black);

            var viewer = new BitmapViewer(bitmap);
            viewer.ShowDialog();
        }

        private void btnRedMatrix_Click(object sender, EventArgs e)
        {
            var matrix = BitmapHelper.GetRedMatrix(_bitmap);
            var mappedMatrix = MathHelper.MapToSymmetricUnitInterval(matrix);

            var viewer = new MatrixViewer(mappedMatrix);
            viewer.ShowDialog();
        }

        private void btnGreenMatrix_Click(object sender, EventArgs e)
        {
            var matrix = BitmapHelper.GetGreenMatrix(_bitmap);
            var mappedMatrix = MathHelper.MapToSymmetricUnitInterval(matrix);

            var viewer = new MatrixViewer(mappedMatrix);
            viewer.ShowDialog();
        }

        private void btnBlueMatrix_Click(object sender, EventArgs e)
        {
            var matrix = BitmapHelper.GetBlueMatrix(_bitmap);
            var mappedMatrix = MathHelper.MapToSymmetricUnitInterval(matrix);

            var viewer = new MatrixViewer(mappedMatrix);
            viewer.ShowDialog();
        }

        private void btnAlphaMatrix_Click(object sender, EventArgs e)
        {
            var matrix = BitmapHelper.GetAlphaMatrix(_bitmap);
            var mappedMatrix = MathHelper.MapToSymmetricUnitInterval(matrix);

            var viewer = new MatrixViewer(mappedMatrix);
            viewer.ShowDialog();
        }
    }
}
